/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xuexiang.xuidemo.slice.refresh;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import com.xuexiang.xui_lib.stickycustom.StickListScrollManager;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.item.StickyItem;
import com.xuexiang.xuidemo.util.ToastUtil;
import com.xuexiang.xuidemo.util.WebUtils;

/**
 * guozhaocai
 *
 * @since 2021-04-21
 */
public class StickyCustomAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private StickyListProvider stickyListProvider;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_fraction_sticky_custom);
        initComponent();
    }

    /**
     * 初始化列表布局
     */
    public void initComponent() {
        Text headTitleText = (Text) findComponentById(ResourceTable.Id_tv_title);
        Text titleText = (Text) findComponentById(ResourceTable.Id_text_title);
        Image imageBack = (Image) findComponentById(ResourceTable.Id_image_back);

        titleText.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);
        titleText.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);
        titleText.startAutoScrolling();
        Text actionText = (Text) findComponentById(ResourceTable.Id_tv_action);
        DependentLayout dependentLayout = (DependentLayout) findComponentById(ResourceTable.Id_dependLayout);
        Component headerContainer = findComponentById(ResourceTable.Id_header_container);
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_listContainer_view);
        stickyListProvider = new StickyListProvider(DemoDataProvider.getStickyDemoData());
        listContainer.setItemProvider(stickyListProvider);
        if (stickyListProvider.getData().get(0).isHeadSticky()) {
            headTitleText.setText(stickyListProvider.getData().get(0).getHeadTitle());
        }
        StickListScrollManager stickListScrollManager = new StickListScrollManager(listContainer, headerContainer, StickListScrollManager.STICKY_TYPE_MONITOR_SCROLL_STATE);
        stickListScrollManager.setHeaderComponentOffsetY(dependentLayout.getHeight());
        stickListScrollManager.setTitleChangeListener(new StickListScrollManager.TitleChangeListener() {
            @Override
            public void onTitleChangeCallback(String title) {
                headTitleText.setText(title);
            }
        });
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int position, long l) {
                StickyItem item = stickyListProvider.getItem(position);

                if (item != null && item.getNewInfo() != null) {
                    WebUtils.goWeb(getContext(), item.getNewInfo().getDetailUrl());
                }
            }
        });

        actionText.setClickedListener(this);
        headerContainer.setClickedListener(this);
        dependentLayout.setClickedListener(this);
        imageBack.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_image_back:
                onBackPressed();
                break;
            case ResourceTable.Id_tv_title:

                break;
            case ResourceTable.Id_header_container:

                break;
            case ResourceTable.Id_tv_action:
                ToastUtil.showToast(getContext(), "点击更多");
                break;
            default:
                break;
        }

    }
}
